package org.jeecg.modules.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.system.entity.ZkEquipmentMaintain;
import org.jeecg.modules.system.entity.ZkEquipmentMaintainDetail;
import org.jeecg.modules.system.service.IZkEquipmentMaintainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description: 设备保养表
 * @Author: jeecg-boot
 * @Date:   2024-04-28
 * @Version: V1.0
 */
@Api(tags="设备保养表")
@RestController
@RequestMapping("/ZkEquipmentMaintain/zkEquipmentMaintain")
@Slf4j
public class ZkEquipmentMaintainController extends JeecgController<ZkEquipmentMaintain, IZkEquipmentMaintainService> {
	@Autowired
	private IZkEquipmentMaintainService zkEquipmentMaintainService;
	
	/**
	 * 分页列表查询
	 *
	 * @param zkEquipmentMaintain
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	//@AutoLog(value = "设备保养表-分页列表查询")
	@ApiOperation(value="设备保养表-分页列表查询", notes="设备保养表-分页列表查询")
	@GetMapping(value = "/list")
	public Result<IPage<ZkEquipmentMaintain>> queryPageList(ZkEquipmentMaintain zkEquipmentMaintain,
															@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
															@RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
															HttpServletRequest req) {
		QueryWrapper<ZkEquipmentMaintain> queryWrapper = QueryGenerator.initQueryWrapper(zkEquipmentMaintain, req.getParameterMap());
		Page<ZkEquipmentMaintain> page = new Page<ZkEquipmentMaintain>(pageNo, pageSize);
		IPage<ZkEquipmentMaintain> pageList = zkEquipmentMaintainService.page(page, queryWrapper);
		return Result.OK(pageList);
	}
	
	/**
	 *   添加
	 *
	 * @param zkEquipmentMaintain
	 * @return
	 */
	@AutoLog(value = "设备保养表-添加")
	@ApiOperation(value="设备保养表-添加", notes="设备保养表-添加")
	@PostMapping(value = "/add")
	public Result<String> add(@RequestBody ZkEquipmentMaintain zkEquipmentMaintain) {
		zkEquipmentMaintainService.save(zkEquipmentMaintain);
		return Result.OK("添加成功！");
	}
	
	/**
	 *  编辑
	 *
	 * @param zkEquipmentMaintain
	 * @return
	 */
	@AutoLog(value = "设备保养表-编辑")
	@ApiOperation(value="设备保养表-编辑", notes="设备保养表-编辑")
	@RequestMapping(value = "/edit", method = {RequestMethod.PUT,RequestMethod.POST})
	public Result<String> edit(@RequestBody ZkEquipmentMaintain zkEquipmentMaintain) {
		zkEquipmentMaintainService.updateById(zkEquipmentMaintain);
		return Result.OK("编辑成功!");
	}
	
	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "设备保养表-通过id删除")
	@ApiOperation(value="设备保养表-通过id删除", notes="设备保养表-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<String> delete(@RequestParam(name="id",required=true) String id) {
		zkEquipmentMaintainService.removeById(id);
		return Result.OK("删除成功!");
	}
	
	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "设备保养表-批量删除")
	@ApiOperation(value="设备保养表-批量删除", notes="设备保养表-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<String> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.zkEquipmentMaintainService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}
	
	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	//@AutoLog(value = "设备保养表-通过id查询")
	@ApiOperation(value="设备保养表-通过id查询", notes="设备保养表-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<ZkEquipmentMaintain> queryById(@RequestParam(name="id",required=true) String id) {
		ZkEquipmentMaintain zkEquipmentMaintain = zkEquipmentMaintainService.getById(id);
		if(zkEquipmentMaintain==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(zkEquipmentMaintain);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param zkEquipmentMaintain
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, ZkEquipmentMaintain zkEquipmentMaintain) {
        return super.exportXls(request, zkEquipmentMaintain, ZkEquipmentMaintain.class, "设备保养表");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, ZkEquipmentMaintain.class);
    }

	 /**
	  * 设备保养根据当前登录人员和时间查询
	  */
	 @AutoLog(value = "查询需要保养的设备")
	 @ApiOperation(value="查询需要保养的设备", notes="查询需要保养的设备")
	 @GetMapping(value = "/queryZkEquipmentMaintain")
	 public Result<?> queryZkEquipmentMaintain(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {
		 //根据时间和人员ID查询需要保养的设备列表
		 List<ZkEquipmentMaintain> pageList = zkEquipmentMaintainService.queryPageListVo(pageNo,pageSize);

		for(ZkEquipmentMaintain zkEquipmentMaintain:pageList){
			//判断时间差
			Date date=new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String lastTime = format.format(date);

			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			LocalDate staDate = LocalDate.parse(lastTime, formatter);
			LocalDate endDate = LocalDate.parse(zkEquipmentMaintain.getLastMaintainTime(), formatter);
			long days = endDate.until(staDate, ChronoUnit.DAYS);

			//每天
			if(zkEquipmentMaintain.getMaintainTime()==1&&days<1){
				pageList.remove(zkEquipmentMaintain);
			}
			//每周
		    else if(zkEquipmentMaintain.getMaintainTime()==2&&days<7){
				pageList.remove(zkEquipmentMaintain);
			}
		    //半月
			else if(zkEquipmentMaintain.getMaintainTime()==3&&days<15){
				pageList.remove(zkEquipmentMaintain);
			}
			//每月
			else if(zkEquipmentMaintain.getMaintainTime()==4&&days<30){
				pageList.remove(zkEquipmentMaintain);
			}
			//半年
			else if(zkEquipmentMaintain.getMaintainTime()==5&&days<182){
				pageList.remove(zkEquipmentMaintain);
			}
			//每年
			else if(zkEquipmentMaintain.getMaintainTime()==6&&days<365){
				pageList.remove(zkEquipmentMaintain);
			}
		}

		 return Result.OK(pageList);
	 }

	/**
	 * 审批界面/维修（根据当前登录人和状态查询）
	 */

	/**
	 * 设备保养根据当前登录人员和时间查询
	 */
	@AutoLog(value = "查询审批界面保养信息")
	@ApiOperation(value="查询审批界面保养信息", notes="查询审批界面保养信息")
	@GetMapping(value = "/queryZkEquipmentService")
	public Result<?> queryZkEquipmentMaintainDetailPageList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
											  				@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		IPage<ZkEquipmentMaintainDetail> pageList=this.zkEquipmentMaintainService.queryZkEquipmentMaintainDetail(pageNo,pageSize);

		return Result.OK(pageList);
	}

	/**
	 * 设备保养根据当前登录人员和时间查询
	 */
	@AutoLog(value = "查询发起界面保养信息")
	@ApiOperation(value="查询发起界面保养信息", notes="查询发起界面保养信息")
	@GetMapping(value = "/queryZkEquipmentMaintainDetailList")
	public Result<?> queryZkEquipmentMaintainDetailList(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
														@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		IPage<ZkEquipmentMaintainDetail> pageList=this.zkEquipmentMaintainService.queryZkEquipmentMaintainList(pageNo,pageSize);

		return Result.OK(pageList);
	}

	/**
	 * 抄送任务
	 */
	@AutoLog(value = "抄送任务保养")
	@ApiOperation(value="抄送任务保养", notes="抄送任务保养")
	@GetMapping(value = "/queryZkEquipmentMaintainCc")
	public Result<?> queryZkEquipmentMaintainCc(@RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
												@RequestParam(name="pageSize", defaultValue="10") Integer pageSize) {

		IPage<ZkEquipmentMaintainDetail> pageList=this.zkEquipmentMaintainService.queryZkEquipmentMaintainCc(pageNo,pageSize);

		return Result.OK(pageList);
	}

	/**
	 * 查询全部数据修改保养状态
	 */
	@AutoLog(value = "修改保养状态")
	@ApiOperation(value="修改保养状态", notes="修改保养状态")
	@GetMapping(value = "/queryZkEquipmentMaintainAll")
	public Result<?> queryZkEquipmentMaintainAll() {
		List<ZkEquipmentMaintain> list=this.zkEquipmentMaintainService.queryZkEquipmentMaintain();
		for(ZkEquipmentMaintain zkEquipmentMaintain:list){
			//判断时间差
			Date date=new Date();
			SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
			String lastTime = format.format(date);

			DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd");
			LocalDate staDate = LocalDate.parse(lastTime, formatter);
			LocalDate endDate = LocalDate.parse(zkEquipmentMaintain.getLastMaintainTime(), formatter);
			long days = endDate.until(staDate, ChronoUnit.DAYS);

			//每天
			if(zkEquipmentMaintain.getMaintainTime()==1&&days>1){
				zkEquipmentMaintain.setMaintainStatus(0);
				zkEquipmentMaintainService.updateById(zkEquipmentMaintain);
			}
			//每周
			else if(zkEquipmentMaintain.getMaintainTime()==2&&days>7){
				zkEquipmentMaintain.setMaintainStatus(0);
				zkEquipmentMaintainService.updateById(zkEquipmentMaintain);
			}
			//半月
			else if(zkEquipmentMaintain.getMaintainTime()==3&&days>15){
				zkEquipmentMaintain.setMaintainStatus(0);
				zkEquipmentMaintainService.updateById(zkEquipmentMaintain);
			}
			//每月
			else if(zkEquipmentMaintain.getMaintainTime()==4&&days>30){
				zkEquipmentMaintain.setMaintainStatus(0);
				zkEquipmentMaintainService.updateById(zkEquipmentMaintain);
			}
			//半年
			else if(zkEquipmentMaintain.getMaintainTime()==5&&days>182){
				zkEquipmentMaintain.setMaintainStatus(0);
				zkEquipmentMaintainService.updateById(zkEquipmentMaintain);
			}
			//每年
			else if(zkEquipmentMaintain.getMaintainTime()==6&&days>365){
				zkEquipmentMaintain.setMaintainStatus(0);
				zkEquipmentMaintainService.updateById(zkEquipmentMaintain);
			}
		}

		return Result.OK("数据刷新成功!");
	}

	/**
	 * 保养柱状图PC端首页(根据保养周期统计数量)
	 */
	@AutoLog(value = "保养柱状图PC端首页(根据保养周期统计数量)")
	@ApiOperation(value="保养柱状图PC端首页(根据保养周期统计数量)", notes="保养柱状图PC端首页(根据保养周期统计数量)")
	@GetMapping(value = "/pcCount")
	public Result<?> pcCount() {

		List<Map<Integer,Integer>> coutNum=this.zkEquipmentMaintainService.pcCount();

		return Result.OK(coutNum);
	}

 }
